.class public Lorg/microemu/cldc/ssl/Connection;
.super Lorg/microemu/cldc/socket/SocketConnection;
.source "Connection.java"

# interfaces
.implements Ljavax/microedition/io/SecureConnection;
.implements Lorg/microemu/cldc/ClosedConnection;


# instance fields
.field private securityInfo:Ljavax/microedition/io/SecurityInfo;


# direct methods
.method public constructor <init>()V
    .locals 1

    .prologue
    .line 51
    invoke-direct {p0}, Lorg/microemu/cldc/socket/SocketConnection;-><init>()V

    .line 52
    const/4 v0, 0x0

    iput-object v0, p0, Lorg/microemu/cldc/ssl/Connection;->securityInfo:Ljavax/microedition/io/SecurityInfo;

    .line 53
    return-void
.end method


# virtual methods
.method public close()V
    .locals 1
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 97
    iget-object v0, p0, Lorg/microemu/cldc/ssl/Connection;->socket:Ljava/net/Socket;

    invoke-virtual {v0}, Ljava/net/Socket;->close()V

    .line 98
    return-void
.end method

.method public getSecurityInfo()Ljavax/microedition/io/SecurityInfo;
    .locals 7
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 101
    iget-object v2, p0, Lorg/microemu/cldc/ssl/Connection;->securityInfo:Ljavax/microedition/io/SecurityInfo;

    if-nez v2, :cond_1

    .line 102
    iget-object v2, p0, Lorg/microemu/cldc/ssl/Connection;->socket:Ljava/net/Socket;

    check-cast v2, Ljavax/net/ssl/SSLSocket;

    invoke-virtual {v2}, Ljavax/net/ssl/SSLSocket;->getSession()Ljavax/net/ssl/SSLSession;

    move-result-object v1

    .line 104
    .local v1, session:Ljavax/net/ssl/SSLSession;
    invoke-interface {v1}, Ljavax/net/ssl/SSLSession;->getPeerCertificates()[Ljava/security/cert/Certificate;

    move-result-object v0

    .line 105
    .local v0, certs:[Ljava/security/cert/Certificate;
    array-length v2, v0

    if-nez v2, :cond_0

    .line 106
    new-instance v2, Ljava/io/IOException;

    invoke-direct {v2}, Ljava/io/IOException;-><init>()V

    throw v2

    .line 109
    :cond_0
    new-instance v3, Lorg/microemu/cldc/SecurityInfoImpl;

    invoke-interface {v1}, Ljavax/net/ssl/SSLSession;->getCipherSuite()Ljava/lang/String;

    move-result-object v4

    invoke-interface {v1}, Ljavax/net/ssl/SSLSession;->getProtocol()Ljava/lang/String;

    move-result-object v5

    new-instance v6, Lorg/microemu/cldc/CertificateImpl;

    const/4 v2, 0x0

    aget-object v2, v0, v2

    check-cast v2, Ljava/security/cert/X509Certificate;

    invoke-direct {v6, v2}, Lorg/microemu/cldc/CertificateImpl;-><init>(Ljava/security/cert/X509Certificate;)V

    invoke-direct {v3, v4, v5, v6}, Lorg/microemu/cldc/SecurityInfoImpl;-><init>(Ljava/lang/String;Ljava/lang/String;Ljavax/microedition/pki/Certificate;)V

    iput-object v3, p0, Lorg/microemu/cldc/ssl/Connection;->securityInfo:Ljavax/microedition/io/SecurityInfo;

    .line 115
    .end local v1           #session:Ljavax/net/ssl/SSLSession;
    .end local v0           #certs:[Ljava/security/cert/Certificate;
    :cond_1
    iget-object v2, p0, Lorg/microemu/cldc/ssl/Connection;->securityInfo:Ljavax/microedition/io/SecurityInfo;

    return-object v2
.end method

.method public open(Ljava/lang/String;)Ljavax/microedition/io/Connection;
    .locals 9
    .parameter "name"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 57
    invoke-static {}, Lorg/microemu/cldc/http/Connection;->isAllowNetworkConnection()Z

    move-result v7

    if-nez v7, :cond_0

    .line 58
    new-instance v7, Ljava/io/IOException;

    const-string v8, "No network"

    invoke-direct {v7, v8}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V

    throw v7

    .line 61
    :cond_0
    const/16 v7, 0x3a

    invoke-virtual {p1, v7}, Ljava/lang/String;->lastIndexOf(I)I

    move-result v4

    .line 62
    .local v4, portSepIndex:I
    add-int/lit8 v7, v4, 0x1

    invoke-virtual {p1, v7}, Ljava/lang/String;->substring(I)Ljava/lang/String;

    move-result-object v7

    invoke-static {v7}, Ljava/lang/Integer;->parseInt(Ljava/lang/String;)I

    move-result v3

    .line 63
    .local v3, port:I
    const-string v7, "ssl://"

    invoke-virtual {v7}, Ljava/lang/String;->length()I

    move-result v7

    invoke-virtual {p1, v7, v4}, Ljava/lang/String;->substring(II)Ljava/lang/String;

    move-result-object v2

    .line 66
    .local v2, host:Ljava/lang/String;
    const/4 v7, 0x1

    new-array v6, v7, [Ljavax/net/ssl/TrustManager;

    const/4 v7, 0x0

    new-instance v8, Lorg/microemu/cldc/ssl/Connection$1;

    invoke-direct {v8, p0}, Lorg/microemu/cldc/ssl/Connection$1;-><init>(Lorg/microemu/cldc/ssl/Connection;)V

    aput-object v8, v6, v7

    .line 81
    .local v6, trustAllCerts:[Ljavax/net/ssl/TrustManager;
    :try_start_0
    const-string v7, "SSL"

    invoke-static {v7}, Ljavax/net/ssl/SSLContext;->getInstance(Ljava/lang/String;)Ljavax/net/ssl/SSLContext;

    move-result-object v5

    .line 82
    .local v5, sc:Ljavax/net/ssl/SSLContext;
    const/4 v7, 0x0

    new-instance v8, Ljava/security/SecureRandom;

    invoke-direct {v8}, Ljava/security/SecureRandom;-><init>()V

    invoke-virtual {v5, v7, v6, v8}, Ljavax/net/ssl/SSLContext;->init([Ljavax/net/ssl/KeyManager;[Ljavax/net/ssl/TrustManager;Ljava/security/SecureRandom;)V

    .line 83
    invoke-virtual {v5}, Ljavax/net/ssl/SSLContext;->getSocketFactory()Ljavax/net/ssl/SSLSocketFactory;

    move-result-object v1

    .line 84
    .local v1, factory:Ljavax/net/ssl/SSLSocketFactory;
    invoke-virtual {v1, v2, v3}, Ljavax/net/ssl/SSLSocketFactory;->createSocket(Ljava/lang/String;I)Ljava/net/Socket;

    move-result-object v7

    iput-object v7, p0, Lorg/microemu/cldc/ssl/Connection;->socket:Ljava/net/Socket;
    :try_end_0
    .catch Ljava/security/NoSuchAlgorithmException; {:try_start_0 .. :try_end_0} :catch_0
    .catch Ljava/security/KeyManagementException; {:try_start_0 .. :try_end_0} :catch_1

    .line 91
    return-object p0

    .line 85
    .end local v5           #sc:Ljavax/net/ssl/SSLContext;
    .end local v1           #factory:Ljavax/net/ssl/SSLSocketFactory;
    :catch_0
    move-exception v7

    move-object v0, v7

    .line 86
    .local v0, ex:Ljava/security/NoSuchAlgorithmException;
    new-instance v7, Ljava/io/IOException;

    invoke-virtual {v0}, Ljava/security/NoSuchAlgorithmException;->toString()Ljava/lang/String;

    move-result-object v8

    invoke-direct {v7, v8}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V

    throw v7

    .line 87
    .end local v0           #ex:Ljava/security/NoSuchAlgorithmException;
    :catch_1
    move-exception v7

    move-object v0, v7

    .line 88
    .local v0, ex:Ljava/security/KeyManagementException;
    new-instance v7, Ljava/io/IOException;

    invoke-virtual {v0}, Ljava/security/KeyManagementException;->toString()Ljava/lang/String;

    move-result-object v8

    invoke-direct {v7, v8}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V

    throw v7
.end method
